package com.znwxss.config;

import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 七牛云oss配置
 * @Author: hb
 * @CreateTime: 2023-12-25 06:20
 * @Version: 1.0
 */
@Data
@Configuration
public class QiNiuYunConfig {

    /**
     * 七牛域名domain
     */
    @Value("${oss.qiniu.url}")
    private String url;

    /**
     * 七牛ACCESS_KEY
     */
    @Value("${oss.qiniu.accessKey}")
    private String accessKey;

    /**
     * 七牛SECRET_KEY
     */
    @Value("${oss.qiniu.secretKey}")
    private String secretKey;

    /**
     * 七牛空间名
     */
    @Value("${oss.qiniu.bucket}")
    private String bucketName;

    private com.qiniu.storage.Configuration qnConfig() {
        return new com.qiniu.storage.Configuration(Zone.autoZone());//根据自己机房的位置来选择
    }

    /*
     构建一个七牛上传工具实例
     */
    @Bean
    public UploadManager uploadManager() {
        return new UploadManager(qnConfig());
    }

    /*
     认证信息实例
     */
    @Bean
    public Auth auth() {
        return Auth.create(accessKey, secretKey);
    }

    /*
     构建七牛空间管理实例
     */
    @Bean
    public BucketManager bucketManager() {
        return new BucketManager(auth(), qnConfig());
    }

}
